import request from '../../util/request';
import NProgress from 'nprogress'
import { Notification } from 'element-ui';

const mdedit = {
    state: {
        id: null,
        url: null,
        doc: '# 欢迎使用',
        author: null,
        createDate: null,
        updater: null,
        updateDate: null
    },
    mutations: {
        setDoc: (state, doc) => {
            state.doc = doc;
        },
        setUrl: (state, url) => {
            state.url = url;
        },
        setId: (state, id) => {
            state.id = id;
        },
        setAuthor: (state, author) => {
            state.author = author;
        },
        setCreateDate: (state, createDate) => {
            state.createDate = createDate;
        },
        setUpdater: (state, updater) => {
            state.updater = updater;
        },
        setUpdateDate: (state, updateDate) => {
            state.updateDate = updateDate;
        },
        reflashState: (state)=>{
            state.id = null;
            state.url = null;
            state.doc = "# 欢迎使用";
            state.author = null;
            state.createDate = null;
            state.updater = null;
            state.updateDate = null;
        }
    },
    actions: {
        loadDocument(context, url) {
            NProgress.start();
            context.commit('setDoc', '加载中...');
            request({
                url: '/document/' + url,
                method: "get"
            }).then(function (resp) {
                if (resp.errmsg == 'ok') {
                    context.commit('setId', resp.data.id == 0 ? null : resp.data.id);
                    context.commit('setUrl', url);
                    context.commit('setDoc', resp.data.content);
                    context.commit('setAuthor', resp.data.author);
                    context.commit('setCreateDate', resp.data.createDate);
                    context.commit('setUpdater', resp.data.updater);
                    context.commit('setUpdateDate', resp.data.updateDate);
                    NProgress.done();
                } else {
                    console.log(resp);
                }
            }).catch(function (e) {
                console.log(e);
            });
        },
        updateDocument(context, content) {
            NProgress.start();
            if (!context.state.url) {
                Notification.warning('请先选择文档');
                return;
            }
            request({
                url: '/document',
                method: "post",
                data: {
                    id: context.state.id,
                    projectId: context.rootState.catalog.projectId,
                    url: context.state.url,
                    content: content
                }
            }).then(function (resp) {
                if (resp.errmsg == 'ok') {
                    context.commit('setId', resp.data.id == 0 ? null : resp.data.id);
                    context.commit('setUrl', resp.data.url);
                    context.commit('setDoc', resp.data.content);
                    context.commit('setAuthor', resp.data.author);
                    context.commit('setCreateDate', resp.data.createDate);
                    context.commit('setUpdater', resp.data.updater);
                    context.commit('setUpdateDate', resp.data.updateDate);
                    NProgress.done();
                    Notification.success({
                        title: '保存',
                        message: '保存成功',
                        duration: 1000
                    });
                } else {
                    console.log(resp);
                }
            }).catch(function (e) {
                console.log(e);
            });
        }
    }
}

export default mdedit
